package cn.bugstack.domain.order.service;


import cn.bugstack.domain.order.adapter.port.IProductPort;
import cn.bugstack.domain.order.adapter.repository.IOrderRepository;
import cn.bugstack.domain.order.model.aggregate.CreateOrderAggregate;
import cn.bugstack.domain.order.model.entity.OrderEntity;
import cn.bugstack.domain.order.model.entity.PayOrderEntity;
import cn.bugstack.domain.order.model.entity.ProductEntity;
import cn.bugstack.domain.order.model.entity.ShopCartEntity;
import cn.bugstack.domain.order.model.valueOBJ.OrderStatusVO;
import com.alipay.api.AlipayApiException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 丨一
 * @version 1.0
 * @title AbstractOrderService
 * @description: 依赖倒置
 * @date 2024/11/12 18:27
 */
@Slf4j
public abstract class AbstractOrderService implements IOrderService {
    
    protected final IOrderRepository repository;
    
    protected final IProductPort port;
    

    public AbstractOrderService(IOrderRepository repository, IProductPort port) {
        this.repository = repository;
        this.port = port;
    }


    @Override
    public PayOrderEntity createOrder(ShopCartEntity shopCartEntity) throws AlipayApiException {
        //1.查询当前用户是否存在当前商品未支付的订单或者掉单订单
        OrderEntity unpaidOrderEntity = repository.queryUnPayOrder(shopCartEntity);
                
        if (unpaidOrderEntity != null && OrderStatusVO.PAY_WAIT.getCode().equals(unpaidOrderEntity.getOrderStatusVO().getCode())) {
            log.info("存在未支付的订单,userId : {},productId:{},orderId:{}", unpaidOrderEntity.getUserId(),unpaidOrderEntity.getProductId(),unpaidOrderEntity.getOrderId());
            return PayOrderEntity.builder()
                    .orderId(unpaidOrderEntity.getOrderId())
                    .payUrl(unpaidOrderEntity.getPayUrl())
                    .build();
        } else if (unpaidOrderEntity != null && OrderStatusVO.CREATE.getCode().equals(unpaidOrderEntity.getOrderStatusVO().getCode())) {
            //还有一种情况: 已经生成了订单,但是支付宝接口超时没有生成 payUrl,此时 payUrl 为空 -> 未创建支付单的订单
//            log.info("存在未创建支付单的订单, userId:{},productId:{},orderId:{}", unpaidOrderEntity.getUserId(),unpaidOrderEntity.getProductId(),unpaidOrderEntity.getUserId());
//            //为该订单创建支付单
//            PayOrderEntity payOrderEntity = doPayOrder(unPayOrder.getProductId(), unPayOrder.getProductName(), unPayOrder.getOrderId(), unPayOrder.getTotalAmount());
//            return PayOrderRes.builder()
//                    .orderId(payOrder.getOrderId())
//                    .payUrl(payOrder.getPayUrl())
//                    .build();
        }
        //查询商品 & 创建订单
        //TODO:编写查询方法
        ProductEntity productEntity = port.queryProductByProductId(shopCartEntity.getProductId());
        
        OrderEntity orderEntity = CreateOrderAggregate.buildOrderEntity(productEntity.getProductId(), productEntity.getProductName());
        CreateOrderAggregate createOrderAggregate = CreateOrderAggregate.builder()
                .orderEntity(orderEntity)
                .productEntity(productEntity)
                .userId(shopCartEntity.getUserId())
                .build();
        
        
        
        //TODO:创建支付单 支付宝沙箱
        //创建订单
        doSaveOrder(createOrderAggregate);
        
        
        return null;
        //return doPayOrder(createOrderAggregate);
    }


    protected abstract PayOrderEntity doPayOrder(CreateOrderAggregate createOrderAggregate) throws AlipayApiException;

    protected abstract void doSaveOrder(CreateOrderAggregate createOrderAggregate);
}
